#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include<QLabel>
#include<QPushButton>
#include<string>
#include<iostream>
#include<QDebug>
#include<QKeyEvent>
#include <QPainter>
#include <stdlib.h>
#include <time.h>
#include<QDialog>
#include<QMessageBox>
#include<QTimerEvent>

#define ZOOM 1

#define POINT 100

int X,Y;
int nowTime = 0,steps=0;
int timerId;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //新游戏
    connect(ui->actiond,&QAction::triggered,[=](){
        this->CreatNewGame();
    });

    //游戏说明
    connect(ui->action1,&QAction::triggered,[=](){
        QMessageBox::information(this,"游戏说明","按“↑”、“↓”、“←”、“→”对空白方块进行操作");
    });
    this->setFixedSize(480*ZOOM,640*ZOOM);
    this->setWindowIcon(QPixmap(":/game/MNLS.png"));
    this->setWindowTitle("拼图");
    //打印输出框

    init();

    //显示拼图
    this->update();
}
void Widget::init(){
    X=NUM-1;
    Y=NUM-1;
    //设置界面
    for(int i=0;i<NUM;i++){
        for(int j=0;j<NUM;j++){
            Game[i][j]=i*NUM+j;
        }
    }
    //设置步数和时间的字体
    QPalette pe;
    pe.setColor(QPalette::WindowText, Qt::white);
    ui->label->move(90,530);
    ui->label_2->move(340,530);
    ui->label->setPalette(pe);
    ui->label_2->setPalette(pe);


    CreatNewGame();
//    timerId=startTimer(1000);
}

void Widget::CreatNewGame(){
    //打乱的拼图
    steps=0;
    ui->label_2->setText(QString::number(steps));
    srand((unsigned)time(NULL));
    //增加游戏打乱随机度
    for(int i=0;i<RANDTIME;i++){
           int r=rand() % NUM;
        if(r==0&&Y>0){
            GoUp();
        }else if(r==1&&Y<NUM-1){
            GoDown();
        }else if(r==2&&X>0){
            GoLeft();
        }else if(r==3&&X<NUM-1){
            GoRight();
        }

    }


    //游戏开始
}

//计时器
void Widget::timerEvent(QTimerEvent *){

    ui->label->setText(QString::number(nowTime++));

}
void Widget::paintEvent(QPaintEvent*)
{
    //背景
    QPainter painter (this);
    QPixmap pix,pix2;
    pix.load(":/game/beijing.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    painter.drawPixmap((this->width()-420)/2,(this->height()-420)/2,420,420,pix2);
    QPixmap timeBox,stepBox;
    //游戏主体

    for(int i=0;i<NUM;i++)
    {
        for(int j=0;j<NUM;j++)
        {
            //计算当前图片名
            char nowPic[20]=":/game/block/";

            Game[i][j]>=10?nowPic[13]=Game[i][j]/10+'0',nowPic[14]=Game[i][j]%10+'0':nowPic[13]=Game[i][j]%10+'0';
            strcat(nowPic,".png");
            //qDebug()<<"PicName:"<<nowPic<<"  Number:"<<Game[i][j];

            QPixmap temPic;
            temPic.load(nowPic);
            painter.drawPixmap((this->width()-NUM*POINT)/2+j*POINT,(this->height()-NUM*POINT)/2+i*POINT,POINT,POINT,temPic);
        }
    }
}

//判断游戏是否胜利
bool Widget::ifWin()
{
    bool win=true;
    for(int i=0;i<NUM;i++){
        for(int j=0;j<NUM;j++){
            if(Game[i][j]!=i*4+j){
                win=false;
                break;
            }
        }
    }
    //胜利弹出对话框
    if(win){


        QString winMess="好耶！完成了拼图 ";


        int result=QMessageBox::information(this,"恭喜！",winMess);
        if(result==QMessageBox::Ok){
            this->CreatNewGame();
        }

    }

    return win;
}




void Widget::GoUp(){
    //交换数组元素
    int temp=Game[Y][X];
    Game[Y][X]=Game[Y-1][X];
    Game[Y-1][X]=temp;
    Y--;
   //重新显示
    this->update();
}
void Widget::GoDown(){
    //交换数组元素
    int temp=Game[Y][X];
    Game[Y][X]=Game[Y+1][X];
    Game[Y+1][X]=temp;
    Y++;
   //重新显示
    this->update();
}
void Widget::GoLeft(){
    //交换数组元素
    int temp=Game[Y][X];
    Game[Y][X]=Game[Y][X-1];
    Game[Y][X-1]=temp;
    X--;
   //重新显示
    this->update();
}
void Widget::GoRight(){
    //交换数组元素
    int temp=Game[Y][X];
    Game[Y][X]=Game[Y][X+1];
    Game[Y][X+1]=temp;
    X++;
   //重新显示
    this->update();
}


//按键操作
void Widget::keyPressEvent(QKeyEvent  *event)
{
    this->setFocusPolicy(Qt::StrongFocus);


     if(event->key()==Qt::Key_Up)
    {
        qDebug()<<"按下了 ↑按键";
        if(Y>0){
            GoUp();

            steps++;
            ui->label_2->setText(QString::number(steps));
        }
     }else if(event->key()==Qt::Key_Down){
         qDebug()<<"按下了 ↓按键";
         if(Y<NUM-1){
               GoDown();

               steps++;
               ui->label_2->setText(QString::number(steps));
         }
     }else if(event->key()==Qt::Key_Left){
         qDebug()<<"按下了 ←按键";
         if(X>0){
            GoLeft();

            steps++;
            ui->label_2->setText(QString::number(steps));
         }
     }else if(event->key()==Qt::Key_Right){
         qDebug()<<"按下了 →按键";
         if(X<NUM-1){
             GoRight();

             steps++;
             ui->label_2->setText(QString::number(steps));
         }
     }
    this->ifWin();
}
Widget::~Widget()
{
    delete ui;
}

